import request from './request'

//获取后端返回的token
// export function getTokenDetail(yzhExchangeToken) {
//   // return request({
//   //   url: `waterway/yzhIntegrate/getUserInfo`,
//   //   method: 'get',
//   //   params: {
//   //     yzhExchangeToken: yzhExchangeToken,
//   //     source: 'small',
//   //   },
//   // }).then(response => {
//   //   return response.data.token;
//   // });
//   return new Promise((resolve, reject) => {
//     request({
//       url: `waterway/yzhIntegrate/getUserInfo`,
//       method: 'get',
//       params: {
//         yzhExchangeToken: yzhExchangeToken,
//         source: 'small',
//       },
//     }).then(response => {
//       resolve(response.data.token);
//     });
//   });
// }

export async function getTokenDetail(yzhExchangeToken) {
    await request({
      url: `title/middle/bridge/service/waterway/yzhIntegrate/getUserInfo`,
      method: 'get',
      params: {
        yzhExchangeToken: yzhExchangeToken,
        source: 'small',
      },
    }).then(response => {
      const { data } = response;
      sessionStorage.setItem('access_token', data.token);
    });
}

export async function getYthToken(ythToken) {
  await request({
    url: `service/waterway/yzhIntegrate/getUserInfoYTH`,
    method: 'get',
    params: {
      ythToken: ythToken,
      source: "small",
    }
  }).then(response => {
    const { data } = response;
    if(data){
      sessionStorage.setItem('access_token', data.token);
      sessionStorage.setItem('watermarkTxt', data.watermarkTxt);
    }
  })
}

export function getTokenInfo() {
  let currentUrl = window.location.href;
  const queryString = currentUrl.split('?')[1] || '';
  // 创建一个URLSearchParams对象
  const params = new URLSearchParams(queryString);
  // 获取yzhExchangeToken的值
  const yzhExchangeToken = params.get('YZH_EXCHANGE_TOKEN');
  const token = params.get('access_token');
  const ythToken = params.get('token')?params.get('token').split(' ')[1]:'';
  const source = params.get('source');
  return { yzhExchangeToken, token, ythToken, source };
}
